package view.backend.graveyard;

import exceptionManager.ExceptionManager;
import java.sql.SQLException;
import javax.swing.*;
import model.GraveYard;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminGraveYard
{
    private JPanel graveYardPanel;
    private GraveYard graveYardObject;
    private AdminGraveYardOverview adminAdminGraveYardOverview;
    private AdminGraveYardAddress adminAddress;
    private AdminGraveYardName adminName;

    public AdminGraveYard()
    {
        graveYardObject = new GraveYard();
        
        graveYardPanel = new JPanel(null);
        graveYardPanel.setBounds(395, 320, 650, 500);
        graveYardPanel.setOpaque(false);
        
        adminName = new AdminGraveYardName(this);
        JPanel namePanel = adminName.getPanel();
        namePanel.setBounds(0, 0, 300, 85);
        namePanel.setVisible(true);
        
        adminAddress = new AdminGraveYardAddress(this);
        JPanel addressPanel = adminAddress.getPanel();
        addressPanel.setBounds(0, 90, 300, 135);
        addressPanel.setVisible(true);
        
        adminAdminGraveYardOverview = new AdminGraveYardOverview(this);
        adminAdminGraveYardOverview.getPanel().setVisible(true);
        
        graveYardPanel.add(adminAdminGraveYardOverview.getPanel());
        graveYardPanel.add(namePanel);
        graveYardPanel.add(addressPanel);
        graveYardPanel.setVisible(false);
    }
    
    public void doLookUp()
    {
        if (graveYardObject.isCompleted() && graveYardObject.getAddress().isCompleted())
        {
            try
            {
                graveYardObject.setAddress(model.handlers.AddressHandler.getExistingAddress(graveYardObject.getAddress()));
            } 
            catch (SQLException ex)
            {
                ExceptionManager.handleSQLException(ex);
            } 
            catch (ClassNotFoundException ex)
            {
                ExceptionManager.handleClassNotFoundException(ex);
            }        
        }
    }
    
    public void updateOverviewPanel()
    {
        adminAdminGraveYardOverview.uddateLabelsFromModel();
    }
    
    public void reset()
    {
        graveYardObject = null;
        graveYardObject = new GraveYard();
        adminAdminGraveYardOverview.uddateLabelsFromModel();
        adminName.setFieldsFromModel();
        adminAddress.setFieldsFromModel();        
    }

    public GraveYard getGraveYardObject()
    {
        return graveYardObject;
    }

    public JPanel getPanel()
    {
        return graveYardPanel;
    }
}